package com.stoken.web.control;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.stoken.domain.Notice;
import com.stoken.service.NoticeService;
import com.symbio.commons.Compositor;
import com.symbio.commons.Compositor.CompositorType;
import com.symbio.commons.Page;
import com.symbio.utils.WebUtils;

@Controller
@RequestMapping("/notice")
public class NoticeControl {
	
	@Autowired
	private NoticeService noticeService;
	
	/**
	 * 增加公告
	 */
	@RequestMapping(value ="/backstage/add",method=RequestMethod.POST)
	public String addUser(Notice notice,HttpServletRequest request,ModelMap modeMape){	
		notice.setCreateTime(new Date());
		noticeService.save(notice);			
		
		return "/backstage/notice/noticeList";
	}
	
	@RequestMapping("/backstage/loadNoticeList")
	public String loadNoticeList(){		
		return "/backstage/notice/noticeList";
	}
	
	/**
	 * 后台展示公告数据
	 * @param request
	 * @param modeMape
	 * @return
	 */
	@RequestMapping("/backstage/adminNoticeData")
	public String adminListPage(HttpServletRequest request,ModelMap modeMape)
	{
	
		Page<Notice> pageData = new Page<Notice>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("createTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
		pageData = noticeService.find(pageData);		
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		modeMape.addAttribute("pagination", page.showPages());
	    
		return "/backstage/notice/noticeData";
	}
	
	/**
	 * 删除新闻
	 * @param request
	 * @param modeMape
	 * @param id
	 * @return
	 */
	@RequestMapping("/backstage/delete")
	public String  deleteNotice(HttpServletRequest request,ModelMap modeMape,Long noticeId){
		noticeService.delete(noticeId);
		return "/backstage/notice/noticeList";
	}
	
	
	/**
	 * 加载新增新闻页面
	 * @return
	 */
	@RequestMapping("/backstage/loadAddPage")
	public String loadAddNoticePage(){
		return "/backstage/notice/addNotice";
		
	}
	
	
	/**
	 * 查询公告
	 * @param request
	 * @param modeMape
	 * @return
	 */
	@RequestMapping("/showNotice")
	public String showNotice(HttpServletRequest request,ModelMap modeMape,String nid)
	{
		if(StringUtils.isNotBlank(nid))
		{
			Notice notice = noticeService.get(new Long(nid));
			modeMape.addAttribute("notice", notice);
		}
		return "/operat/newsContent";
	}
	
	/**
	 * 查询公告
	 * @param request
	 * @param modeMape
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request,ModelMap modeMape)
	{
		return "/operat/news";
	}
	
	/**
	 * 分页查询公告
	 * @param request
	 * @param modeMape
	 * @return
	 */
	@RequestMapping("/listPage")
	public String listPage(HttpServletRequest request,ModelMap modeMape)
	{
	
		Page<Notice> pageData = new Page<Notice>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("createTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
		pageData = noticeService.find(pageData);		
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		modeMape.addAttribute("pagination", page.showPages());
	    
		return "/operat/newsPage";
	}
	

}
